<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GSAP TextPlugin 示例（需单独加载）</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: #f0f0f0;
            flex-direction: column;
            gap: 30px;
        }
        .box {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            text-align: center;
        }
        #counter, #text-changer {
            font-size: 2em;
            font-weight: bold;
            color: #333;
        }
        button {
            padding: 10px 20px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box">
    <h2>数字递增动画</h2>
    <div id="counter">0</div>
    <button onclick="animateCounter()">开始计数</button>
</div>

<div class="box">
    <h2>文本动态切换</h2>
    <div id="text-changer">欢迎使用 GSAP！</div>
    <button onclick="changeText()">切换文本</button>
</div>

<!-- 必须加载 GSAP 核心 + TextPlugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/TextPlugin.min.js"></script>

<script>
    // 注册插件（必需！）
    gsap.registerPlugin(TextPlugin);

    // 数字递增动画
    function animateCounter() {
        gsap.to("#counter", {
            text: "100",       // 使用TextPlugin的text属性
            duration: 2,
            snap: { text: 1 }, // 整数步进
            ease: "power1.out"
        });
    }

    // 文本切换
    const texts = ["Hello", "Bonjour", "こんにちは", "你好"];
    let currentIndex = 0;
    function changeText() {
        currentIndex = (currentIndex + 1) % texts.length;
        gsap.to("#text-changer", {
            text: texts[currentIndex],
            duration: 0.5,
            ease: "power1.inOut"
        });
    }
</script>
</body>
</html>